/**
 * 
 */
(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('MyMessageController', MyMessageController);

    MyMessageController.$inject = ['$scope', '$state','$http', 'MyMessage', 'ParseLinks', 'AlertService', 'paginationConstants', 'pagingParams'];

    function MyMessageController ($scope, $state, $http,MyMessage, ParseLinks, AlertService, paginationConstants, pagingParams) {
    	 var vm = this;

         vm.loadPage = loadPage;
         vm.predicate = pagingParams.predicate;
         vm.reverse = pagingParams.ascending;
         vm.transition = transition;
         vm.itemsPerPage = paginationConstants.itemsPerPage;
         vm.send=send;
         vm.content='';
         vm.messages=[];
         loadAll();

         function loadAll () {
        	 MyMessage.query({
                 page: pagingParams.page - 1,
                 size: vm.itemsPerPage,
                 sort: sort()
             }, onSuccess, onError);
             function sort() {
                 var result = [vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc')];
                 if (vm.predicate !== 'id') {
                     result.push('id');
                 }
                 return result;
             }
             function onSuccess(data, headers) {
                 vm.links = ParseLinks.parse(headers('link'));
                 vm.totalItems = headers('X-Total-Count');
                 vm.queryCount = vm.totalItems;
                 vm.messages = data;
                 vm.page = pagingParams.page;
             }
             function onError(error) {
                 AlertService.error(error.data.message);
             }
         }

         function loadPage(page) {
             vm.page = page;
             vm.transition();
         }

         function transition() {
             $state.transitionTo($state.$current, {
                 page: vm.page,
                 sort: vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc'),
                 search: vm.currentSearch
             });
         }
         
         function send(){
        	 $http({
        			method: 'GET',
        			url: '/api/my/message/send',
        			params:{'content':vm.content}
        		}).then(function successCallback(response) {
        				vm.messages.splice(0,0, response.data);
        				console.log(vm.messages);
        			}, function errorCallback(response) {
        				// 请求失败执行代码
        				console.log('fail');
        		});
         }
    }
})();
